package mobweb.booktrack;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BookListAct extends Activity {
    private SQLiteHelper sqlite_helper;
    private SQLiteDatabase sqldb;
    private LinearLayout mainLayout;

    // TODO Selecting read books: load them into book activity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_book_list_);
	// Show the Up button in the action bar.
	// getActionBar().setDisplayHomeAsUpEnabled(true);
	sqlite_helper = new SQLiteHelper(this.getApplicationContext());
	sqldb = sqlite_helper.getReadableDatabase();
	// HOWTO QUERY: sqldb.rawQuery("<SQL QUERY>", <something I dont know>);
	// sqldb.query();
	// TODO Load book list from DB
	loadBooks();
	sqldb.close();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	// Inflate the menu; this adds items to the action bar if it is present.
	getMenuInflater().inflate(R.menu.activity_book_list_, menu);
	return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	case android.R.id.home:
	    // This ID represents the Home or Up button. In the case of this
	    // activity, the Up button is shown. Use NavUtils to allow users
	    // to navigate up one level in the application structure. For
	    // more details, see the Navigation pattern on Android Design:
	    //
	    // http://developer.android.com/design/patterns/navigation.html#up-vs-back
	    //
	    NavUtils.navigateUpFromSameTask(this);
	    return true;
	}
	return super.onOptionsItemSelected(item);
    }

    private void loadBooks() {
	// TODO load the books into the UI
	Cursor c = sqldb.query("booksRead", new String[] { "_id", "bookName",
		"description", "flag", "ISBN", "author", "category" }, null,
		null, null, null, null, null);
	c.moveToLast();
	int last = c.getPosition();
	c.moveToFirst();
	while (c.getPosition() <= last) {
	    long id = c.getLong(0);
	    String bookName = c.getString(1);
	    String description = c.getString(2);
	    int flag = c.getInt(3);
	    String ISBN = c.getString(4);
	    String author = c.getString(5);
	    String category = c.getString(6);
	    Book b = new Book(bookName, description, author, category, ISBN,
		    flag, id);
	    addBook(b);
	    c.moveToNext();
	}
	if (c.getCount() == 0) {
	    mainLayout = (LinearLayout) findViewById(R.id.MainLinearLayout);
	    TextView noBooks = new TextView(getBaseContext());
	    noBooks.setText("You have no books in the list");
	    noBooks.setGravity(Gravity.CENTER_HORIZONTAL);
	    noBooks.setTextSize(30);
	    noBooks.setTextColor(Color.BLACK);
	    mainLayout.addView(noBooks);
	}
	c.close();
    }

    private void addBook(final Book b) {
    	    Display display = getWindowManager().getDefaultDisplay();
    	    int width = display.getWidth();
    	    //int height = size.y;
    	    int maxTextPixels = width*70/100;
		mainLayout = (LinearLayout) findViewById(R.id.MainLinearLayout);
		// TODO Add book b to UI
		LinearLayout childLayout = new LinearLayout(getBaseContext());
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
		childLayout.setLayoutParams(lp);
		childLayout.setOrientation(LinearLayout.HORIZONTAL);
		childLayout.setGravity(Gravity.LEFT);
		childLayout.setClickable(true);

		ImageView bookImage = new ImageView(getBaseContext());
		// TODO query google books api here for an image
		childLayout.addView(bookImage);

		LinearLayout child2 = new LinearLayout(getBaseContext());
		child2.setOrientation(LinearLayout.VERTICAL);
		
		Button but = new Button(getBaseContext());
		but.setText("Details");
		but.setOnClickListener(new OnClickListener() {

		    @Override
		    public void onClick(View v) {
			Intent i = new Intent(getApplicationContext(), BookActivity.class);
			i.putExtra("book", b);
			startActivity(i);
		    }
		    
		});

		if (b.getBookName()!=null) {
        		TextView bookTitle = new TextView(getBaseContext());
        		bookTitle.setText(b.getBookName());
        		bookTitle.setTextSize(20);
        		bookTitle.setTextColor(Color.BLACK);
        		bookTitle.setMaxWidth(maxTextPixels);
        		child2.addView(bookTitle);
		}
		
		if(b.getAuthor()!=null) {
        		TextView bookAuthor = new TextView(getBaseContext());
        		bookAuthor.setText(b.getAuthor());
        		bookAuthor.setTextSize(16);
        		bookAuthor.setTextColor(Color.BLACK);
        		bookAuthor.setMaxWidth(maxTextPixels);
        		child2.addView(bookAuthor);
		}

		if (b.getShortDescription()!=null) {
        		TextView bookDesc = new TextView(getBaseContext());
        		bookDesc.setText(b.getShortDescription());
        		bookDesc.setMaxWidth(maxTextPixels);
        		child2.addView(bookDesc);
		}

		TextView bookState = new TextView(getBaseContext());
		switch (b.getFlag()) {
		case Book.READ:
			bookState.setText("You have read this book");
			bookState.setTextColor(Color.GREEN);
			break;
		case Book.READING:
			bookState.setText("You are reading this book");
			bookState.setTextColor(Color.YELLOW);
			break;
		case Book.WISH_TO_READ:
			bookState.setText("You want to read this book");
			bookState.setTextColor(Color.RED);
			break;
		}
		bookState.setTextSize(12);
		bookState.setMaxWidth(maxTextPixels);
		bookState.setShadowLayer(0.5f, 0.5f, 0.5f, Color.BLACK);
		child2.addView(bookState);
		
		childLayout.addView(child2);
		childLayout.addView(but);
		
		View divider = new View(getBaseContext());
		divider.setBackgroundColor(Color.BLACK);
		LayoutParams lp2 = new LayoutParams(LayoutParams.MATCH_PARENT,1);
		divider.setLayoutParams(lp2);
		divider.setPadding(2, 2, 2, 2);
		
		mainLayout.addView(divider);
		mainLayout.addView(childLayout);
	}
    @Override
    protected void onRestart() {
        super.onRestart();
        sqlite_helper = new SQLiteHelper(this.getApplicationContext());
	sqldb = sqlite_helper.getReadableDatabase();
	mainLayout.removeAllViews();
        loadBooks();
        sqldb.close();
    }
}
